package com.znyj.cloud.device.dao;

import com.znyj.cloud.device.entity.ReportEntity;
import com.znyj.cloud.device.sqlHelper.SqlProvider;
import org.apache.ibatis.annotations.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;


/**
 * Created by gaoliwei02 on 17/5/31.
 */

@Repository
public interface ReportDao {

    public static final String SELECT_KEYS = " " +
            "id," +
            "imei," +
            "mac," +
            "device_id," +
            "poiid," +
            "online_status," +
            "report_time," +
            "register_time," +
            "ota_time," +
            "order_create_time_a," +
            "order_create_time_b," +
            "order_duration_sec_a," +
            "order_duration_sec_b," +
            "sig_quality," +
            "error_info," +
            "battery_level," +
            "battery_temperature," +
            "battery_status," +
            "battery_cycle_time," +
            "battery_voltage," +
            "location," +
            "is_gpsfix," +
            "m_version," +
            "f_version," +
            "misc_message," +
            "user_id_a," +
            "user_id_b," +
            "order_id_a," +
            "order_id_b," +
            "status_a," +
            "status_b," +
            "charge_in";
    public static final String INSERT_KEYS = " " +
            "imei," +
            "mac," +
            "device_id," +
            "poiid," +
            "online_status," +
            "report_time," +
            "register_time," +
            "ota_time," +
            "order_create_time_a," +
            "order_create_time_b," +
            "order_duration_sec_a," +
            "order_duration_sec_b," +
            "sig_quality," +
            "error_info," +
            "battery_level," +
            "battery_temperature," +
            "battery_status," +
            "battery_cycle_time," +
            "battery_voltage," +
            "location," +
            "is_gpsfix," +
            "m_version," +
            "f_version," +
            "misc_message," +
            "user_id_a," +
            "user_id_b," +
            "order_id_a," +
            "order_id_b," +
            "status_a," +
            "status_b," +
            "charge_in";
    public static final String INSERT_VALUES = " " +
            "#{imei}," +
            "#{mac}," +
            "#{deviceId}," +
            "#{poiid}," +
            "#{onlineStatus}," +
            "#{reportTime}," +
            "#{registerTime}," +
            "#{otaTime}," +
            "#{orderCreateTimeA}," +
            "#{orderCreateTimeB}," +
            "#{orderDurationSecA}," +
            "#{orderDurationSecB}," +
            "#{sigQuality}," +
            "#{errorInfo}," +
            "#{batteryLevel}," +
            "#{batteryTemperature}," +
            "#{batteryStatus}," +
            "#{batteryCycleTime}," +
            "#{batteryVoltage}," +
            "#{location}," +
            "#{isGpsFix}," +
            "#{mVersion}," +
            "#{fVersion}," +
            "#{miseMessage}," +
            "#{userIdA}," +
            "#{userIdB}," +
            "#{orderIdA}," +
            "#{orderIdB}," +
            "#{statusA}," +
            "#{statusB}," +
            "#{chargeIn}";
    public static final String SIMPLE_SELECT_KEYS = "id,device_id";

    @Insert("insert into cdb_report (" + INSERT_KEYS + ") values (" + INSERT_VALUES + ") ")
    public int insert(ReportEntity entity);

    @Update("update cdb_report set " +
            "imei=#{imei}," +
            "mac=#{mac}," +
            "poiid=#{poiid}," +
            "online_status=#{onlineStatus}," +
            "report_time=#{reportTime}," +
            "register_time=#{registerTime}," +
            "ota_time=#{otaTime}," +
            "order_create_time_a=#{orderCreateTimeA}," +
            "order_create_time_b=#{orderCreateTimeB}," +
            "order_duration_sec_a=#{orderDurationSecA}," +
            "order_duration_sec_b=#{orderDurationSecB}," +
            "sig_quality=#{sigQuality}," +
            "error_info=#{errorInfo}," +
            "battery_level=#{batteryLevel}," +
            "battery_temperature=#{batteryTemperature}," +
            "battery_status=#{batteryStatus}," +
            "battery_cycle_time=#{batteryCycleTime}," +
            "battery_voltage=#{batteryVoltage}," +
            "location=#{location}," +
            "is_gpsfix=#{isGpsFix}" +
            "m_version=#{mVersion}," +
            "f_version=#{fVersion}," +
            "misc_message=#{miseMessage}," +
            "user_id_a=#{userIdA}," +
            "user_id_b=#{userIdB}," +
            "order_id_a=#{orderIdA}," +
            "order_id_b=#{orderIdB}," +
            "status_a=#{statusA}," +
            "status_b=#{statusB}," +
            "charge_in=#{chargeIn} " +
            "where device_id=#{deviceId}")
    public int updateAll(ReportEntity entity);

    @Select("select " + SIMPLE_SELECT_KEYS + " from cdb_report where device_id=#{deviceId}")
    public ReportEntity findByDeviceId(@Param("deviceId") String deviceId);

    @Update("update cdb_report set " +
            "imei=#{imei}," +
            "mac=#{mac}," +
            "online_status=#{onlineStatus}," +
            "report_time=#{reportTime}," +
            "sig_quality=#{sigQuality}," +
            "error_info=#{errorInfo}," +
            "battery_level=#{batteryLevel}," +
            "battery_temperature=#{batteryTemperature}," +
            "battery_status=#{batteryStatus}," +
            "battery_cycle_time=#{batteryCycleTime}," +
            "battery_voltage=#{batteryVoltage}," +
            "location=#{location}," +
            "is_gpsfix=#{isGpsFix}," +
            "m_version=#{mVersion}," +
            "f_version=#{fVersion}," +
            "misc_message=#{miseMessage}," +
            "status_a=#{statusA}," +
            "status_b=#{statusB}," +
            "charge_in=#{chargeIn} " +
            "where device_id=#{deviceId}")
    public int updateCommonInfo(ReportEntity entity);

    @Update("update cdb_report set online_status=\"offline\"")
    public int initOfflineDevice();


    @Select("select order_id_a from cdb_report where device_id=#{deviceId}")
    public long getChannalAOrderId(@Param("deviceId") String deviceId);

    @Select("select order_id_b from cdb_report where device_id=#{deviceId}")
    public long getChannalBOrderId(@Param("deviceId") String deviceId);

    @Update("update cdb_report set " +
            "order_create_time_a=#{orderCreateTimeA}," +
            "order_duration_sec_a=#{orderDurationSecA}," +
            "user_id_a=#{userIdA}," +
            "order_id_a=#{orderIdA} " +
            "where device_id=#{deviceId}")
    public int updateAInfo(@Param("deviceId") String deviceId,
                           @Param("orderCreateTimeA") Timestamp orderCreateTimeA,
                           @Param("orderDurationSecA") long orderDurationSecA,
                           @Param("userIdA") String userIdA,
                           @Param("orderIdA") long orderIdA);

    @Update("update cdb_report set " +
            "order_create_time_b=#{orderCreateTimeB}," +
            "order_duration_sec_b=#{orderDurationSecB}," +
            "user_id_b=#{userIdB}," +
            "order_id_b=#{orderIdB} " +
            "where device_id=#{deviceId}")
    public int updateBInfo(@Param("deviceId") String deviceId,
                           @Param("orderCreateTimeB") Timestamp orderCreateTimeB,
                           @Param("orderDurationSecB") long orderDurationSecB,
                           @Param("userIdB") String userIdB,
                           @Param("orderIdB") long orderIdB);

    @Update("update cdb_report set " +
            "order_create_time_a=#{orderCreateTimeA}," +
            "order_duration_sec_a=#{orderDurationSecA}," +
            "user_id_a=#{userIdA}," +
            "order_id_a=#{orderIdA}," +
            "order_create_time_b=#{orderCreateTimeB}," +
            "order_duration_sec_b=#{orderDurationSecB}," +
            "user_id_b=#{userIdB}," +
            "order_id_b=#{orderIdB} " +
            "where device_id=#{deviceId}")
    public int updateABInfo(@Param("deviceId") String deviceId,
                            @Param("orderCreateTimeA") Timestamp orderCreateTimeA,
                            @Param("orderDurationSecA") long orderDurationSecA,
                            @Param("userIdA") String userIdA,
                            @Param("orderIdA") long orderIdA,
                            @Param("orderCreateTimeB") Timestamp orderCreateTimeB,
                            @Param("orderDurationSecB") long orderDurationSecB,
                            @Param("userIdB") String userIdB,
                            @Param("orderIdB") long orderIdB
    );

    @Update("update cdb_report set " +
            "ota_time=#{otaTime} " +
            "where device_id=#{deviceId}")
    public int updateOtaTime(@Param("deviceId") String deviceId, @Param("otaTime") Timestamp otaTime);

    @Update("update cdb_report set " +
            "online_status=#{onlineStatus} " +
            "where device_id=#{deviceId}")
    public int updateOnlineStatus(@Param("deviceId") String deviceId, @Param("onlineStatus") String onlineStatus);

    @Select("select count(device_id) from cdb_report where device_id=#{deviceId}")
    public int getDeviceIdNumber(@Param("deviceId") String deviceId);

    @Delete("delete from cdb_report where device_id=#{deviceId}")
    public int deleteDeviceId(@Param("deviceId") String deviceId);

    @SelectProvider(type = SqlProvider.class, method = "findBindPoiid")
    public List<Long> findBindPoiid(String deviceId);

    @Update("update cdb_report set " +
            "poiid=#{poiid} " +
            "where device_id=#{deviceId}")
    public int updatePoiid(@Param("poiid") long poiid,
                           @Param("deviceId") String deviceId);
}